import { getToken } from '@/http/common'
export function uploadImageToQiniu(imageFile) {
  return new Promise(async (resolve, reject) => {
    uni.showLoading({ title: '上传中...', mask: true })
    // 生成上传Token
    const { data } = await getToken()
    // 开始上传
    uni.uploadFile({
      url: 'https://up.qiniu.com',
      filePath: imageFile.path,
      name: 'file',
      formData: {
        token: data.token,
      },
      success(uploadRes) {
        if (uploadRes.statusCode === 200) {
          let data = JSON.parse(uploadRes.data)
          uni.hideLoading()
          resolve(data)
        } else {
          uni.showModal({
            title: '上传失败',
            content: '图片上传失败，请稍后再试',
            showCancel: false,
          })
          reject(new Error('上传失败'))
        }
      },
      fail() {
        uni.hideLoading()
        uni.showModal({
          title: '上传失败',
          content: '图片上传失败，请稍后再试',
          showCancel: false,
        })
        reject(new Error('上传失败'))
      },
    })
  })
}
